package org.suigara.d3.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {
	public static void delete(File file) throws IOException {
		if (file.exists()) {
			return;
		}
		if (file.isFile()) {
			file.delete();
		} else {
			File[] listFiles = file.listFiles();
			if (listFiles != null) {
				for (File file2 : listFiles) {
					delete(file2);
				}
			}
			file.delete();
		}

	}

	public static String readFile(File file) throws IOException {
		try (FileReader fr = new FileReader(file);
				BufferedReader br = new BufferedReader(fr);) {
			String line;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
				sb.append("\n");
			}
			return sb.toString();
		}
	}

	public static void writeFile(File file, String content) throws IOException {
		try (FileWriter fw = new FileWriter(file);
				BufferedWriter bw = new BufferedWriter(fw);) {
			bw.write(content);
			bw.flush();
		}
	}
}
